/*******************************************************************************
 * Copyright 2011-2013 Sergey Tarasevich
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.visikard.utility;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import com.nostra13.universalimageloader.core.process.BitmapProcessor;
import com.visikard.application.VKRegApplication;
import com.visikard.ui.R;

public class MyPostProcessorBitmap implements BitmapProcessor {

	int ratio;
	int outputWidth;
	int outputHeight;
	int roundType;
	int shadowPercent;
	int roundPercent;
	int shadowResId;
	Context context;
	
	

	public MyPostProcessorBitmap(int ratio, int roundType, int roundPercent, int shadowPercent,
			int shadowResId, int outputWidth, int outputHeight) {
		this.ratio = ratio;
		this.roundType = roundType;
		this.roundPercent = roundPercent;
		this.shadowPercent = shadowPercent;
		this.shadowResId = shadowResId;
		this.outputWidth = outputWidth;
		this.outputHeight = outputHeight;
	}

	@Override
	public Bitmap process(Bitmap bitmap) {
		//crop image by ratio
		if( ratio > 0 ) {
			bitmap = BitmapUtils.cropBitmapByRatio(bitmap, ratio);
		}
		
		/*
		 * scale down bitmap to expected width
		 * only scale down - not scale up
		 */			
//		if (outputWidth > 0 && bitmap.getWidth() > outputWidth) {
//			int outHeight = (int) (outputWidth * (bitmap.getHeight() / (float) bitmap.getWidth()));
//			bitmap = Bitmap.createScaledBitmap(bitmap, outputWidth, outHeight, true);
//		}
		if (outputWidth > 0 && outputHeight > 0)
			bitmap = createFramePhoto(bitmap);
		//round corner
		if( roundType != BitmapUtils.ROUND_NONE) {
			if(roundPercent == 0)
				roundPercent = BitmapUtils.CORNER_PERCENT;
			bitmap = BitmapUtils.getRoundedCornerBitmap(bitmap, roundType, roundPercent);
		}
		
		//add shadow to result bitmap			
		if( shadowPercent > 0 ) {
			if( shadowPercent > 0 ) {
				if(shadowResId == 0){
					shadowResId = R.drawable.kard_shadow_default_vertical;
				}
				bitmap = BitmapUtils.addShadow(bitmap, shadowPercent, 
						BitmapFactory.decodeResource(VKRegApplication.instance.getResources(), shadowResId));
				
			}
		}
		return bitmap;
	}
	
	private Bitmap createFramePhoto(Bitmap bitmap) {
		Bitmap output;
		if(!Utils.hasHoneycomb()){
			output = Bitmap.createBitmap(outputWidth,
					outputHeight,
				Config.RGB_565);
		} else {
			output = Bitmap.createBitmap(outputWidth,
					outputHeight,
					Config.ARGB_8888);
		}
		Canvas canvas = new Canvas(output);
		canvas.drawBitmap(bitmap, null, new Rect(0, 0, outputWidth,
				outputHeight), new Paint(Paint.ANTI_ALIAS_FLAG));
		return output;
	}

}
